﻿using System;
using System.Collections.Generic;
using Battle.Engine.LogicModel;

namespace Battle.Engine.AI
{
    public class EngineAI
    {
        public BaseCompute engine = null;
        public EngineData data = null;
        public FPlayer player = null;
        public EngineAICallback callback = null;

        public bool isRunning = false;
        public bool isRunningWhenPause = false;

        public virtual void Start(EngineData data, CampType camp, BaseCompute engine)
        {
            this.engine = engine;
            this.data = data;
            this.player = this.data.getPlayerByCampType(camp);

            isRunning = false;
        }

        public virtual void Stop()
        {
            isRunning = false;
        }

        public virtual void Update(double deltaTime)
        {

        }

        public virtual void Run()
        {
            isRunning = true;
        }
        public virtual void StopRun()
        {
            isRunning = false;
        }

        public virtual void SetCallback(EngineAICallback callback)
        {
            this.callback = callback;
        }

        public void NextRound()
        {
            if (player.camp == data.curRoundCamp && engine.IsAuto(player.camp))
            {
                Run();
            }
            else {
                StopRun();
            }
        }

        public void FinishRound()
        {
            StopRun();
        }

        public virtual void Pause()
        {
            if (isRunning)//如果pause的时候正在执行，则记录原状态
                isRunningWhenPause = true;
            isRunning = false;
        }

        public virtual void Continue()
        {
            if(isRunningWhenPause)
                isRunning = true;
        }

        /// <summary>
        /// 获得可以释放大招的Actor
        /// </summary>
        /// <returns></returns>
        protected List<FActor> GetUltimateReadyActor(bool isMonsterInclude = true) {
            List<FActor> actors = new List<FActor>();
            foreach (var actor in player.actors) {
                if (actor.state == FActorState.DEAD || actor.state == FActorState.WAEKDEAD)
                    continue;
                if (!isMonsterInclude && actor.heroClass == (int)HeroClass.Monster)
                    continue;
                FSkill ultiSkill = actor.GetFSkill((int)SkillType.Ultimate);
                if (ultiSkill != null && actor.isCanDoSkill(ultiSkill)) {
                    actors.Add(actor);
                }
            }
            actors.Sort(delegate (FActor x, FActor y) {
                return x.MainPosition.ToFPID().CompareTo(y.MainPosition.ToFPID());
            });
            return actors;
        }
    }
}
